import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Form from '../components/form/Form'
import InlineForm from '../components/form/InlineForm'
import Position from '../components/form/Position'
import Validator from '../components/form/Validator'
import MyValidator from '../components/form/MyValidator'
import Home from '../components/Home'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/my-validator'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/form',
    component: Form
  },
  {
    path: '/inline',
    component: InlineForm
  },
  {
    path: '/position',
    component: Position
  },
  {
    path: '/validator',
    component: Validator
  },
  {
    path: '/my-validator',
    component: MyValidator
  }
]

const router = new VueRouter({
  routes
})

//挂在路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
